<?php
namespace App\Service\Admin;

use SebastianBergmann\CodeCoverage\Report\Xml\Totals;

class TongjiService1022 extends BaseService
{
    //统计

    //个人
    public static function person($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh.' as a')
            ->where('a.del', 0);
        if (in_array($pas['urole_id'], [3,4]) && $pas['ustaff_id']) {
        // if ($pas['urole_id']==4 && $pas['ustaff_id']) {
            $db = $db->where('a.staff_id', $pas['ustaff_id']);
        } else if ($pas['is_leader']) {
            $db = $db->where('a.depart_id', $pas['udepart_id']);
        } else if ($pas['depart_id']) {
            $db = $db->where('a.depart_id', $pas['depart_id']);
        }
        if ($pas['staff_id']) {
            $db = $db->where('a.staff_id', $pas['staff_id']);
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01 00:00:01';
            $endtime = $pas['year'].'-'.$month.'-31 23:59:59';
            $db = $db->where('a.uptime', '>=', $starttime);
            $db = $db->where('a.uptime', '<=', $endtime);
        }
        $db = $db->where('a.status', '>', 4);
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('a.in_day', 'desc');
        } else {
            $db = $db->orderBy('a.in_day', 'asc');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '' : $staffOne['title'];
            $shi = floor($v->delay / 60);
            $fen = $v->delay - $shi * 60;
            $delay_str = $shi . '小时' . $fen . '分钟';
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'uname' => $staff_name, 'item_name' => $item_name,
                'delay' => $delay_str, 'in_day' => $v->in_day,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    /** 
     * 项目组
     * 部门经理看本部门的
     * 经理、管理员、审核员看所有的
     * */
    public static function item($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem.' as a')
            ->select('a.*', 'b.item_id', 'b.in_day')
            ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
            ->where('a.del', 0)
            ->where('b.del', 0);
        $db = $db->whereIn('b.status', [5,6,7]);
        if (in_array($pas['role_id'], [1,2,5])) { //管理员|总经理|审核员
            if ($pas['depart_id']) {
                $db = $db->where('a.depart_id', $pas['depart_id']);
            }
            if ($pas['staff_id']) {
                $db = $db->where('a.uid', $pas['staff_id']);
            }
        } else if (in_array($pas['role_id'], [3,4])) {
            if ($pas['is_leader']) { //部门经理
                $db = $db->where('a.depart_id', $pas['udepart_id']);
            } else {
                $db = $db->where('a.uid', $pas['uid']);
            }
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01 00:00:01';
            $endtime = $pas['year'].'-'.$month.'-31 23:59:59';
            $db = $db->where('a.uptime', '>=', $starttime);
            $db = $db->where('a.uptime', '<=', $endtime);
        }
        $db = $db2 = $db->groupBy('a.config_whid');
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('b.in_day', 'desc');
        } else {
            $db = $db->orderBy('b.in_day', 'asc');
        }
        // $total = $db->count();
        $total = count($db2->get());
        // $res = $db->orderBy('a.depart_id', 'asc')
        //     ->orderBy('a.uid', 'asc')
        //     ->orderBy('a.config_whid', 'asc')
        //     ->skip($start)->take($pages['limit'])
        //     ->get();
        $res = $db->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($cfgItemOne) ? '' : $cfgItemOne['title'];
            $itemOnes = self::dbConn(self::$dbYwWhItem.' as a')
                ->select('a.*')
                ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
                ->where('b.item_id', $v->item_id)
                ->where('a.del', 0)
                ->where('a.depart_id', $v->depart_id)
                ->where('a.uid', $v->uid)
                ->where('a.config_whid', $v->config_whid)
                ->where('b.in_day', $v->in_day)
                ->skip(0)->take(5)
                ->orderBy('id', 'desc')
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'item_name' => $item_name,
                'whList' => $itemList, 'delay_str' => $delay_pid_str,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    // //所有项目
    // public static function all($pages, $pas)
    // {
    //     $start = ($pages['start'] - 1) * $pages['limit'];
    //     $db = self::dbConn(self::$dbYwWh.' as a')
    //         ->select('a.*')
    //         // ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
    //         ->where('a.del', 0)
    //         ->where('a.delay', '>', 0);
    //     $db = $db->where('a.status', '>', 4);
    //     if ($pas['depart_id']) {
    //         $db = $db->where('a.depart_id', $pas['depart_id']);
    //     }
    //     // if ($pas['staff_id']) {
    //     //     $db = $db->where('a.uid', $pas['staff_id']);
    //     // }
    //     if ($pas['month']) {
    //         $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
    //         $starttime = $pas['year'].'-'.$month.'-01 00:00:01';
    //         $endtime = $pas['year'].'-'.$month.'-31 23:59:59';
    //         $db = $db->where('a.uptime', '>=', $starttime);
    //         $db = $db->where('a.uptime', '<=', $endtime);
    //     }
    //     $db = $db->groupBy('item_id');
    //     if ($pas['sort_id'] == 2) {
    //         $db = $db->orderBy('a.in_day', 'desc');
    //     } else {
    //         $db = $db->orderBy('a.in_day', 'asc');
    //     }
    //     // $total = $db->count();
    //     $total = count($db->get());
    //     // $res = $db->skip($start)->take($pages['limit'])
    //     //     ->orderBy('a.item_id', 'asc')
    //     //     ->get();
    //     $res = $db->skip($start)->take($pages['limit'])->get();
    //     if (!count($res)) {
    //         return self::toback(-2, '没有数据');
    //     }
    //     $arys = [];
    //     foreach ($res as $v) {
    //         $cfgOne = (array) self::dbConn(self::$dbConfigItem)
    //             ->where('id', $v->item_id)
    //             ->first();
    //         $cfg_name = empty($cfgOne) ? '' : $cfgOne['title'];
    //         $whOnes = self::dbConn(self::$dbYwWh)
    //             ->where('del', 0)
    //             ->where('item_id', $v->item_id)
    //             ->orderBy('id', 'asc')
    //             ->get();
    //         $whids = [];
    //         if (count($whOnes)) {
    //             foreach ($whOnes as $vv) {
    //                 $whids[] = $vv->id;
    //             }
    //         }
    //         $itemOnes = self::dbConn(self::$dbYwWhItem)
    //             ->where('del', 0)
    //             ->whereIn('wh_id', $whids)
    //             ->where('delay', '>', 0)
    //             ->orderBy('id', 'asc')
    //             ->get();
    //         $delay_pid = 0;
    //         $itemList = [];
    //         if (count($itemOnes)) {
    //             foreach ($itemOnes as $vv) {
    //                 $uOne = (array) self::dbConn(self::$dbStaff)
    //                     ->where('id', $vv->uid)
    //                     ->first();
    //                 $uname = empty($uOne) ? '' : $uOne['title'];
    //                 $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
    //                     ->where('id', $vv->config_whid)
    //                     ->first();
    //                 $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
    //                 $shi = floor($vv->delay / 60);
    //                 $fen = $vv->delay - ($shi * 60);
    //                 $delay_str = $shi . '小时' . $fen . '分钟';
    //                 $delay_pid += $vv->delay;
    //                 $itemList[] = [
    //                     'id' => $vv->id, 'wh_name' => $cfgWhName,
    //                     'delay_str' => $delay_str,
    //                     'uname' => $uname,
    //                 ];
    //             }
    //         }
    //         $shi_pid = floor($delay_pid / 60);
    //         $fen_pid = $delay_pid - ($shi_pid * 60);
    //         $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
    //         $arys[] = [
    //             'id' => $v->id, 'item_name' => $cfg_name,
    //             'delay_str' => $delay_pid_str, 'whList' => $itemList,
    //         ];
    //     }
    //     return self::toback(0, '', $arys, $total);
    // }

    //所有项目
    public static function all($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbConfigItem.' as a')
            ->select('a.*')
            ->leftJoin(self::$dbYwWh.' as b', 'a.id', '=', 'b.item_id')
            ->where('a.del', 0)
            ->where('b.delay', '>', 0);
        $db = $db->where('b.status', '>', 4);
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01 00:00:01';
            $endtime = $pas['year'].'-'.$month.'-31 23:59:59';
            $db = $db->where('b.uptime', '>=', $starttime);
            $db = $db->where('b.uptime', '<=', $endtime);
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('b.in_day', 'desc');
        } else {
            $db = $db->orderBy('b.in_day', 'asc');
        }
        $total = count($db->get());
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $whOnes = self::dbConn(self::$dbYwWh)
                ->where('del', 0)
                ->where('item_id', $v->id)
                ->orderBy('id', 'asc')
                ->get();
            $whids = [];
            if (count($whOnes)) {
                foreach ($whOnes as $vv) {
                    $whids[] = $vv->id;
                }
            }
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->whereIn('wh_id', $whids)
                ->where('delay', '>', 0)
                ->orderBy('id', 'asc')
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    // $uOne = (array) self::dbConn(self::$dbStaff)
                    //     ->where('id', $vv->uid)
                    //     ->first();
                    // $uname = empty($uOne) ? '' : $uOne['title'];
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                        // 'uname' => $uname,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            $arys[] = [
                'id' => $v->id, 'item_name' => $v->title,
                'delay_str' => $delay_pid_str, 'whList' => $itemList,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //项目
    public static function huan($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem.' as a')
            ->select('a.*', 'b.item_id', 'b.in_day')
            ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
            ->where('a.del', 0)
            ->where('b.del', 0);
        $db = $db->whereIn('b.status', [5,6,7]);
        // if ($pas['role_id'] == 4) {
            if ($pas['depart_id']) {
                $db = $db->where('a.depart_id', $pas['depart_id']);
            }
            // if ($pas['staff_id']) {
            //     $db = $db->where('a.uid', $pas['staff_id']);
            // }
        // }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01 00:00:01';
            $endtime = $pas['year'].'-'.$month.'-31 23:59:59';
            $db = $db->where('a.uptime', '>=', $starttime);
            $db = $db->where('a.uptime', '<=', $endtime);
        }
        $db = $db2 = $db->groupBy('a.config_whid');
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('b.in_day', 'desc');
        } else {
            $db = $db->orderBy('b.in_day', 'asc');
        }
        // $total = $db->count();
        $total = count($db2->get());
        // $res = $db->orderBy('a.depart_id', 'asc')
        //     ->orderBy('a.uid', 'asc')
        //     ->orderBy('a.config_whid', 'asc')
        //     ->skip($start)->take($pages['limit'])
        //     ->get();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($cfgItemOne) ? '' : $cfgItemOne['title'];
            $itemOnes = self::dbConn(self::$dbYwWhItem.' as a')
                ->select('a.*')
                ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
                ->where('b.item_id', $v->item_id)
                ->where('a.del', 0)
                ->where('a.depart_id', $v->depart_id)
                ->where('a.uid', $v->uid)
                ->where('a.config_whid', $v->config_whid)
                ->where('b.in_day', $v->in_day)
                ->skip(0)->take(5)
                ->orderBy('id', 'desc')
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            // 同比，上一年
            $start = $end = date('Y-m-d');
            if (count($itemOnes)) {
                $start = $itemOnes[count($itemOnes)-1]->uptime;
                $end = $itemOnes[0]->uptime;
            }
            $start = strtotime($start) - 3600 * 24 * 365;
            $end = strtotime($end) - 3600 * 24 * 365;
            $start = date('Y-m-d', $start) . ' 00:00:01';
            $end = date('Y-m-d', $end) . ' 23:59:59';
            $delay_year = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('config_whid', $v->id)
                ->where('uptime', '>=', $start)
                ->where('uptime', '<=', $end)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->sum('delay');
            if ($delay_year) {
                $tongbi = number_format($delay_pid / $delay_year, 2);
            } else {
                $tongbi = $delay_pid * 100;
            }
            $tongbi = $tongbi>0 ? '+' . $tongbi : '-' . $tongbi;
            // 环比，上一个月
            $start = $end = date('Y-m-d');
            if (count($itemOnes)) {
                $start = $itemOnes[count($itemOnes)-1]->uptime;
                $end = $itemOnes[0]->uptime;
            }
            $start = strtotime($start) - 3600 * 24 * 30;
            $end = strtotime($end) - 3600 * 24 * 30;
            $start = date('Y-m-d', $start) . ' 00:00:01';
            $end = date('Y-m-d', $end) . ' 23:59:59';
            $delay_month = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('config_whid', $v->id)
                ->where('uptime', '>=', $start)
                ->where('uptime', '<=', $end)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->sum('delay');
            if ($delay_month) {
                $huanbi = number_format($delay_pid / $delay_month, 2);
            } else {
                $huanbi = $delay_pid * 100;
            }
            $huanbi = $huanbi>0 ? '+' . $huanbi : '-' . $huanbi;
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'item_name' => $item_name,
                'whList' => $itemList, 'delay_str' => $delay_pid_str,
                // 同比上一年，环比上一月
                'delay_pid' => $delay_pid, 'delay_year' => $delay_year,
                'tongbi' => $tongbi, 'huanbi' => $huanbi,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //图
    public static function tu()
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $arys = [
            'bing' => [['xxx', 10], ['zzz', 20]], //项目占比
            'zhu' => [ //工时占比
                'title' => ['张三', '李四'],
                'val' => [[10], [50]],
            ],
        ];
//        return self::toback(0, '', $arys);
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->get();
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $delay = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('item_id', 0)
                    ->sum('delay');
                $arys['bing'][] = [
                    $v->title, $delay,
                ];
            }
        }
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 0)
            ->get();
        if (count($staffOnes)) {
            foreach ($staffOnes as $v) {
                $delay = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('uid', 0)
                    ->sum('delay');
                $arys['zhu']['title'][] = $v->title;
                $arys['zhu']['val'][] = [$delay];
            }
        }
        return self::toback(0, '', $arys);
    }

    //选项
    public static function getOption($pas)
    {
        $arys = ['staff' => [], 'depart' => []];
        if ($pas['role_id'] == 4) {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->where('id', $pas['depart_id'])
                ->get();
            $staffList = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->where('depart_id', $pas['depart_id'])
                ->get();
        } else {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->get();
            if ($pas['depart_id']) {
                $staffList = self::dbConn(self::$dbStaff)
                    ->where('is_able', 0)
                    ->where('depart_id', $pas['depart_id'])
                    ->get();
            } else {
                $staffList = self::dbConn(self::$dbStaff)
                    ->where('is_able', 0)
                    ->get();
            }
        }
        if (count($staffList)) {
            foreach ($staffList as $k => $v) {
                $departOne = (array) self::dbConn(self::$dbStDepart)
                    ->where('id', $v->depart_id)
                    ->first();
                $depart_name = empty($departOne) ? '' : $departOne['title'].'-';
                $staffList[$k]->title = $depart_name.$v->title;
            }
        }
        $arys['depart'] = $departList;
        $arys['staff'] = $staffList;
        return self::toback(0, '', $arys);
    }

    //项目选项
    public static function itemOption($pas)
    {
        $arys = ['staff' => [], 'depart' => []];
        if ($pas['role_id'] == 3) {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->where('id', $pas['depart_id'])
                ->get();
        } else {
            $departList = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->get();
        }
        if ($pas['depart_id']) {
            $staffList = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->where('depart_id', $pas['depart_id'])
                ->get();
        } else {
            $staffList = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->get();
        }
        if (count($staffList)) {
            foreach ($staffList as $k => $v) {
                $departOne = (array) self::dbConn(self::$dbStDepart)
                    ->where('id', $v->depart_id)
                    ->first();
                $depart_name = empty($departOne) ? '' : $departOne['title'].'-';
                $staffList[$k]->title = $depart_name.$v->title;
            }
        }
        $arys['depart'] = $departList;
        $arys['staff'] = $staffList;
        return self::toback(0, '', $arys);
    }

    // 获取要导出的数据
    public static function getOutData($pas)
    {
        $depart_id = $pas['depart_id'];
        $staff_id = $pas['staff_id'];
        $year = $pas['year'];
        $month = $pas['month'];
        $month = strlen($month)==1 ? '0'.$month : $month;
        $uOne = (array) self::dbConn(self::$dbStaff)
            ->where('id', $pas['uid'])
            ->first();
        if ($uOne['role_id'] == 4) {
            $depart_id = $uOne['depart_id'];
            $staff_id = $pas['uid'];
        }
        $headers = ['月份：'.$month.'月','',''];
        $titles = ['部门', '员工', '项目', '工时分配'];
        $days = [];
        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
        for ($i = 1; $i <= $day_num; $i ++) {
            $titles[] = $i;
            $days[] = $i;
        }
        $titles[] = '合计';
        $arys[] = $headers;
        $arys[] = $titles;
        // ddd($arys);
        // 部门数据
        if ($depart_id) {
            $departOnes = self::dbConn(self::$dbStDepart)
                ->where('id', $depart_id)
                ->get();
        } else {
            $departOnes = self::dbConn(self::$dbStDepart)
                ->where('del', 0)
                ->where('is_able', 1)
                ->get();
        }
        if (!count($departOnes)) {
            return self::toback(0, '', $arys);
        }
        foreach ($departOnes as $v) {
            if ($staff_id) {
                $staffOnes = self::dbConn(self::$dbStaff)
                    ->where('is_able', 0)
                    ->where('depart_id', $v->id)
                    ->where('id', $staff_id)
                    ->get();
            } else {
                $staffOnes = self::dbConn(self::$dbStaff)
                    ->where('is_able', 0)
                    ->where('depart_id', $v->id)
                    ->get();
            }
            if (count($staffOnes)) {
            foreach ($staffOnes as $vv) {
                // $arys[] = [$v->title, $vv->title];
                $heji = [];
                $ywwhs = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('depart_id', $v->id)
                    ->where('staff_id', $vv->id)
                    ->groupBy('item_id')
                    ->orderBy('item_id', 'asc')
                    ->get();
                if (count($ywwhs)) {
                foreach ($ywwhs as $vvv) {
                    $is_empty = 0;
                    $depart_name = '';
                    $uname = ''; 
                    $item_name = '';
                    $wh_name = '';
                    $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                        ->where('id', $vvv->item_id)
                        ->first();
                    // $arys[] = [$v->title, $vv->title, $cfgItemOne['title']];
                    $itemOnes = self::dbConn(self::$dbYwWhItem)
                        ->where('del', 0)
                        ->where('depart_id', $v->id)
                        ->where('uid', $vv->id)
                        ->where('wh_id', $vvv->id)
                        ->groupBy('config_whid')
                        ->orderBy('config_whid', 'asc')
                        ->get();
                    if (count($itemOnes)) {
                    foreach ($itemOnes as $vvvv) {
                        $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                            ->where('id', $vvvv->config_whid)
                            ->first();
                        $depart_name = $v->title;
                        $uname = $vv->title; 
                        $item_name = $cfgItemOne['title'];
                        $wh_name = $cfgWhOne['title'];
                        // ddd($depart_name, $uname, $item_name, $wh_name);
                        $rows = [$depart_name, $uname, $item_name, $wh_name];
                        $month_total = 0;
                        foreach ($days as $day) {
                            $heji[$day] = isset($heji[$day]) ? $heji[$day] : 0;
                            $day_str = strlen($day)==1 ? '0'.$day : $day;
                            $daystr = $year.'-'.$month.'-'.$day_str;
                            $daystart = $daystr.' 00:00:01';
                            $dayend = $daystr.' 23:59:59';
                            $delay = self::dbConn(self::$dbYwWhItem)
                                ->where('del', 0)
                                ->where('uid', $vv->id)
                                ->where('config_whid', $vvvv->config_whid)
                                ->where('uptime', '>=', $daystart)
                                ->where('uptime', '<=', $dayend)
                                ->sum('delay');
                            $total = number_format($delay / 60, 1);
                            $month_total += $total;
                            $rows[] = $total;
                            $is_empty = 1;
                        }
                        $rows[] = $month_total;
                        $arys[] = $rows;
                    }
                    }
                    if ($is_empty) {
                        $u_heji = [$depart_name, $uname, $item_name, '合计'];
                        $u_delay = 0;
                        foreach ($days as $day) {
                            $day_heji = $heji[$day];
                            $day_heji_delay = number_format($day_heji / 60, 1);
                            $u_heji[] = $day_heji_delay;
                            $u_delay += $day_heji;
                        }
                        $u_total = number_format($u_delay / 60, 1);
                        $u_heji[] = $u_total;
                        $arys[] = $u_heji;
                     }
                }
                }
            }
            }
        }
        $depart_name = $depart_id ? $depart_name : '所有部门';
        $rst = [
            'data' => $arys,
            'fname' => $depart_name.'('.$pas['month'].'月)工时汇总',
        ];
        return self::toback(0, '', $rst);
    }
}